<?php
function getUrlDbpediaAbstract($term)
{
   $format = 'json';

   $query = "PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX gml: <http://www.opengis.net/gml/>

SELECT ?abstract ?page ?pic WHERE {
?a rdf:type gml:_Feature.
?a dbpedia-owl:abstract ?abstract.
?a foaf:page ?page.
?a foaf:depiction ?pic.
?a rdfs:label \"".$term."\"@en.
FILTER (langMatches(lang(?abstract),\"en\"))
}";

$searchUrl = 'http://dbpedia.org/sparql?'
      .'query='.urlencode($query)
      .'&format='.$format;

   return $searchUrl;
}


function request($url){
 
   // is curl installed?
   if (!function_exists('curl_init')){ 
      die('CURL is not installed!');
   }
   
   // get curl handle
   $ch= curl_init();

   // set request url
   curl_setopt($ch, 
      CURLOPT_URL, 
      $url);

   // return response, don't print/echo
   curl_setopt($ch, 
      CURLOPT_RETURNTRANSFER, 
      true);
 
   /*
   Here you find more options for curl:
   http://www.php.net/curl_setopt
   */    

   $response = curl_exec($ch);
   
   curl_close($ch);
   
   return $response;
}
function printArray($array, $spaces = "")
{
   $retValue = "";
   
   if(is_array($array))
   {  
      $spaces = $spaces
         ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

      $retValue = $retValue."<br/>";

      foreach(array_keys($array) as $key)
      {
         $retValue = $retValue.$spaces
            ."<strong>".$key."</strong>"
            .printArray($array[$key], 
               $spaces);
      }     
      $spaces = substr($spaces, 0, -30);
   }
   else $retValue = 
      $retValue." - ".$array."<br/>";
   
   return $retValue;
}

if(strlen($_GET['s'])==0) { print "Please specify a search string."; }
else {
$term = $_GET['s'];

$requestURL = getUrlDbpediaAbstract($term);

$responseArray = json_decode(
   request($requestURL),
   true); 
if(strlen($responseArray["results"]["bindings"][0]["abstract"]["value"])>0) {
     echo $responseArray["results"]
    ["bindings"][0]
    ["abstract"]["value"]."<br /><br />";
echo "<a href=\"".$responseArray["results"]
    ["bindings"][0]
    ["page"]["value"]."\" target=\"_blank\">More Infos</a><br /><br />";
echo "<img src=\"".$responseArray["results"]
    ["bindings"][0]
    ["pic"]["value"]."\" width=\"400\">";
}
else
{

        // access GeoNames API with featurecodes
        $geonamesurl = "http://api.geonames.org/findNearbyWikipedia?username=mmabc&lat=".$_GET['lat']."&lng=".$_GET['lng'];
        $geonames = file_get_contents($geonamesurl, 0, null, null);
        $geonames_output = new SimpleXMLElement($geonames);
	print "<b>We couldn't match one entry to your sight. Here are the entries to the closest sights:</b><br /><br /><br />";
	foreach($geonames_output->entry as $g) {
	print $g->summary."<br /><br /><a href=\"".$g->wikipediaUrl."\" target=\"_blank\">More Infos</a><br /><br />";
	if(strlen($g->thumbnailImg)>0) {
	echo "<img src=\"".$g->thumbnailImg."\" width=\"400\">";
	}
	print "<br /><hr /><br />";
	}
}

}

?>
